#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int longestArithSeqLength(vector<int>& nums) {
        int n = nums.size();
        if (n <= 2) return n;
        vector<vector<int>> dp(n, vector<int>(n));

        int ret = 0;
        unordered_map<int, int> hash;
        hash[nums[0]] = 0;
        for (int j = 1; j < n - 1; j++)
        {
            for (int i = j + 1; i < n; i++)
            {
                int tgt = nums[j] * 2 - nums[i];
                if (hash.count(tgt))
                {
                    int t = hash[tgt];
                    dp[i][j] = dp[j][t] + 1;
                    ret = max(ret, dp[i][j]);
                }
            }
            hash[nums[j]] = j;
        }

        return 2 + ret;
    }
};